export function tansParams(params: Record<string, any>) {
    let result = ''
    for (const propName of Object.keys(params)) {
        const value = params[propName]
        const part = encodeURIComponent(propName) + '='
        if (value !== null && value !== '' && typeof value !== 'undefined') {
            if (typeof value === 'object') {
                for (const key of Object.keys(value)) {
                    if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
                        const p = `${propName}[${key}]`
                        const subPart = encodeURIComponent(p) + '='
                        result += subPart + encodeURIComponent(value[key]) + '&'
                    }
                }
            } else {
                result += part + encodeURIComponent(value) + '&'
            }
        }
    }
    return result
}

export function getNowDateAndTime(time = new Date()) {
    const d = time
    const year = d.getFullYear()
    const month = d.getMonth() + 1 >= 10 ? d.getMonth() + 1 : '0' + (d.getMonth() + 1)
    const day = d.getDate() >= 10 ? d.getDate() : '0' + d.getDate()
    const hour = d.getHours() >= 10 ? d.getHours() : '0' + d.getHours()
    const minute = d.getMinutes() >= 10 ? d.getMinutes() : '0' + d.getMinutes()
    const second = d.getSeconds() >= 10 ? d.getSeconds() : '0' + d.getSeconds()
    const currentNowDate = `${year}-${month}-${day}`
    const currentNowTime = `${hour}:${minute}:${second}`
    const currentNowDateTime = `${currentNowDate} ${currentNowTime}`
    const nowDay = d.getDay()
    const nowDayObject: Record<number, string> = {
        0: '星期日', 1: '星期一', 2: '星期二', 3: '星期三', 4: '星期四', 5: '星期五', 6: '星期六'
    }
    const currentNowDay = nowDayObject[nowDay]
    return { currentNowDate, currentNowTime, currentNowDay, currentNowDateTime }
}

export function getUuid() {
    let t = new Date().getTime()
    if (window?.performance && typeof window.performance.now === 'function') t += performance.now()
    return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, c => {
        const r = (t + Math.random() * 16) % 16 | 0
        t = Math.floor(t / 16)
        return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16)
    })
}

function pad2(s: number) { return s < 10 ? '0' + s : String(s) }
export function createordernum() {
    const now = new Date()
    const orderCode = `${now.getFullYear()}${pad2(now.getMonth() + 1)}${pad2(now.getDate())}${pad2(now.getHours())}${pad2(now.getMinutes())}${pad2(now.getSeconds())}${Math.round(Math.random() * 1000000)}`
    return orderCode
}

export function parseTime(time?: any, pattern?: string) {
    if (!time) return null
    const fmt = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
    let date: Date
    if (typeof time === 'object') {
        date = time
    } else {
        if (typeof time === 'string' && /^[0-9]+$/.test(time)) time = parseInt(time)
        else if (typeof time === 'string') time = time.replace(/-/g, '/').replace('T', ' ').replace(/\.[\d]{3}/g, '')
        if (typeof time === 'number' && time.toString().length === 10) time = time * 1000
        date = new Date(time)
    }
    const formatObj: any = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() }
    return fmt.replace(/\{(y|m|d|h|i|s|a)+\}/g, (_r, key) => {
        let v = formatObj[key]
        if (key === 'a') return ['日', '一', '二', '三', '四', '五', '六'][v]
        if (_r.length > 0 && v < 10) v = '0' + v
        return v || 0
    })
}
